package goals;

import java.util.Comparator;
import java.util.PriorityQueue;

public class ListOfGoals {

	private PriorityQueue<Goal> priorityQueue, timerQueue;
	private int time;

	public ListOfGoals() {
		priorityQueue = new PriorityQueue<Goal>(5, new Comparator<Goal>() {
			/*
			 * higher-priority goals are smaller so that they are first in the
			 * priority queue
			 */
			@Override
			public int compare(Goal o1, Goal o2) {
				return o2.priority - o1.priority;
			}
		});

		timerQueue = new PriorityQueue<Goal>(5, new Comparator<Goal>() {
			@Override
			public int compare(Goal o1, Goal o2) {
				return o1.reevaluationTime - o2.reevaluationTime;
			}
		});

		time = 0;
	}

	public void add(Goal g) {
		g.reevaluationTime = time + g.getReevaluationTimeSpan();
		priorityQueue.add(g);
		timerQueue.add(g);
	}

	public void clear() {
		priorityQueue.clear();
		timerQueue.clear();
		time = 0;
	}

	public boolean isEmpty() {
		return priorityQueue.isEmpty();
	}

	public Goal peek() {
		return priorityQueue.peek();
	}

	public boolean tickTimeAndCheckGoalTimeouts() {
		time++;
		if (timerQueue.isEmpty()) {
			return false;
		} else {
			return timerQueue.peek().reevaluationTime >= time;
		}
	}

	public Goal poll() {
		Goal g = priorityQueue.poll();
		timerQueue.remove(g);
		return g;
	}

	public String toString() {
		return priorityQueue.toString();
	}
}
